﻿using System.Linq;
using System.Net;
using System.Web.Mvc;
using LunchCommand.Models;
using LunchCommand.ViewModels;

namespace LunchCommand.Controllers
{
    public class VenuesController : BaseController
    {
        // GET: Venues
        public ActionResult Index()
        {
            var venues = DatabaseContext.ActiveVenues;
            return View(venues.ToList());
        }
        
        // GET: Venues/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id != null)
            {
                var venue = DatabaseContext.Venues.Find(id);
                if (venue == null)
                {
                    return HttpNotFound();
                }

                ViewBag.GenreID = new SelectList(DatabaseContext.Genres, "GenreID", "Name", venue.GenreID);
                return View(new VenueViewModel(venue));
            }

            ViewBag.GenreID = new SelectList(DatabaseContext.Genres, "GenreID", "Name", null);
            return this.View(new VenueViewModel(new Venue()) { VenueID = -1 });
        }

        // POST: Venues/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(VenueViewModel venueModel)
        {
            if (ModelState.IsValid)
            {
                if (venueModel.VenueID >= 0)
                {
                    var venue = this.DatabaseContext.Venues.Find(venueModel.VenueID);
                    venueModel.UpdateModel(venue);
                }
                else
                {
                    var venue = new Venue();
                    venue.CreatedBy = this.LunchUser;
                    venueModel.UpdateModel(venue);
                    this.DatabaseContext.Venues.Add(venue);
                }

                this.DatabaseContext.SaveChanges();

                return RedirectToAction("Index");
            }
            ViewBag.GenreID = new SelectList(DatabaseContext.Genres, "GenreID", "Name", venueModel.GenreID);
            return View(venueModel);
        }

        // GET: Venues/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Venue venue = DatabaseContext.Venues.Find(id);
            if (venue == null)
            {
                return HttpNotFound();
            }
            return View(venue);
        }

        // POST: Venues/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Venue venue = DatabaseContext.Venues.Find(id);
            venue.Deleted = true;
            DatabaseContext.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                DatabaseContext.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
